<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.1.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.1.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.1.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.1.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.1.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '6.1.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="先介绍一下.gitignore .gitignore项目里面经常有一些文件是不需要纳入版本控制的，例如我编译后的class文件，或者idea产生的一些配置文件等。 1234567891011121314151617181920212223242526272829.gradle/build/!gradle/wrapper/gradle-wrapper.jar### STS ###.apt_gener">
<meta name="keywords" content="git">
<meta property="og:type" content="article">
<meta property="og:title" content="Git-分支">
<meta property="og:url" content="http://www.saily.top/2017/09/21/git2/index.html">
<meta property="og:site_name" content="帆的博客">
<meta property="og:description" content="先介绍一下.gitignore .gitignore项目里面经常有一些文件是不需要纳入版本控制的，例如我编译后的class文件，或者idea产生的一些配置文件等。 1234567891011121314151617181920212223242526272829.gradle/build/!gradle/wrapper/gradle-wrapper.jar### STS ###.apt_gener">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://www.saily.top/img/git/git2-1.png">
<meta property="og:image" content="http://www.saily.top/img/git/git2-2.png">
<meta property="og:image" content="http://www.saily.top/img/git/git2-3.png">
<meta property="og:image" content="http://www.saily.top/img/git/git2-4.png">
<meta property="og:image" content="http://www.saily.top/img/git/git2-5.png">
<meta property="og:updated_time" content="2017-09-28T02:05:55.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Git-分支">
<meta name="twitter:description" content="先介绍一下.gitignore .gitignore项目里面经常有一些文件是不需要纳入版本控制的，例如我编译后的class文件，或者idea产生的一些配置文件等。 1234567891011121314151617181920212223242526272829.gradle/build/!gradle/wrapper/gradle-wrapper.jar### STS ###.apt_gener">
<meta name="twitter:image" content="http://www.saily.top/img/git/git2-1.png">



  <link rel="alternate" href="/atom.xml" title="帆的博客" type="application/atom+xml" />




  <link rel="canonical" href="http://www.saily.top/2017/09/21/git2/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Git-分支 | 帆的博客</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">帆的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">扬帆起航</p>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>




<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-about">
    <a href="/about/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.saily.top/2017/09/21/git2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="杨帆">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/photo/bug.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="帆的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Git-分支
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
                
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-21T21:35:29+08:00">9月 21 2017</time>
            

            
            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/git/" itemprop="url" rel="index"><span itemprop="name">git</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="post-meta-item-icon"
            >
            <i class="fa fa-eye"></i>
             阅读次数： 
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>先介绍一下<strong>.gitignore</strong></p>
<h2 id="gitignore"><a href="#gitignore" class="headerlink" title=".gitignore"></a>.gitignore</h2><p>项目里面经常有一些文件是不需要纳入版本控制的，例如我编译后的class文件，或者idea产生的一些配置文件等。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">.gradle</span><br><span class="line">/build/</span><br><span class="line">!gradle/wrapper/gradle-wrapper.jar</span><br><span class="line"></span><br><span class="line">### STS ###</span><br><span class="line">.apt_generated</span><br><span class="line">.classpath</span><br><span class="line">.factorypath</span><br><span class="line">.project</span><br><span class="line">.settings</span><br><span class="line">.springBeans</span><br><span class="line"></span><br><span class="line">### IntelliJ IDEA ###</span><br><span class="line">.idea</span><br><span class="line">*.iws</span><br><span class="line">*.iml</span><br><span class="line">*.ipr</span><br><span class="line"></span><br><span class="line">### NetBeans ###</span><br><span class="line">nbproject/private/</span><br><span class="line">build/</span><br><span class="line">nbbuild/</span><br><span class="line">dist/</span><br><span class="line">nbdist/</span><br><span class="line">.nb-gradle/</span><br><span class="line"></span><br><span class="line">rebel.xml</span><br><span class="line"></span><br><span class="line">/out</span><br></pre></td></tr></table></figure>
<p>上面是我的一个项目的配置，看得出来是有一些规则的，使用起来很简单，将这些规则保存到一个<code>.gitignore</code>中后，在项目里面这些文件或者文件夹里面的所有修改操作都不会被纳入到git版本控制中，也不会提交。</p>
<a id="more"></a>
<h3 id="规则"><a href="#规则" class="headerlink" title="规则"></a>规则</h3><ul>
<li><strong>*.a</strong> 忽略所有.a结尾的文件</li>
<li><strong>!lib.a</strong> 但lib.a除外</li>
<li><strong>/TODO</strong> 仅仅忽略项目根目录下的TODO文件，但不包括subdir/TODO</li>
<li><strong>build/</strong> 忽略build/目录下的所有文件</li>
<li><strong>doc/*.txt</strong> 会忽略doc/notes.txt但不包括doc/server/arch.txt</li>
</ul>
<h2 id="Git分支"><a href="#Git分支" class="headerlink" title="Git分支"></a>Git分支</h2><p>分支就是当你正在开发的时候，你可以基于当前的工作副本新开一条开发线，2边都可以进行单独的开发，当开发到某一个阶段的时候，可以合并到一起，这样2个分支的修改内容都能保留。</p>
<p>————— master<br>————— new_branch</p>
<h3 id="git-branch"><a href="#git-branch" class="headerlink" title="git branch"></a>git branch</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) git branch</span><br><span class="line">* master</span><br></pre></td></tr></table></figure>
<p>这个命令可以显示当前所在分支，当使用<code>git init</code>创建仓库的时候，git默认会为我们创建一个master分支。</p>
<h4 id="创建分支"><a href="#创建分支" class="headerlink" title="创建分支"></a>创建分支</h4><p>创建一个新的分支并切换到新的分支上</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) git branch new_branch</span><br><span class="line">➜  mygit git:(master) git branch</span><br><span class="line">* master</span><br><span class="line">  new_branch</span><br><span class="line">➜  mygit git:(master) git checkout new_branch</span><br><span class="line">Switched to branch <span class="string">'new_branch'</span></span><br></pre></td></tr></table></figure>
<p>这个时候2个分支上的文件还是一模一样的。</p>
<h4 id="切换分支"><a href="#切换分支" class="headerlink" title="切换分支"></a>切换分支</h4><p>在<strong>new_branch</strong>分支上创建一个文件并提交，切换到master上查看，是没有这个文件的，这也说明2个不同分支上的文件是独立的，修改是互不影响的。</p>
<p>切换分支的命令：<code>git checkout</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(new_branch) <span class="built_in">echo</span> <span class="string">'test new branch'</span> &gt; test4.txt</span><br><span class="line">➜  mygit git:(new_branch) ✗ git add test4.txt </span><br><span class="line">➜  mygit git:(new_branch) ✗ git commit -m <span class="string">'add a new file'</span></span><br><span class="line">[new_branch 8013cc2] add a new file</span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br><span class="line"> create mode 100644 test4.txt</span><br><span class="line">➜  mygit git:(new_branch) git status</span><br><span class="line">On branch new_branch</span><br><span class="line">nothing to commit, working tree clean</span><br><span class="line">➜  mygit git:(new_branch) ls</span><br><span class="line">mydir               settings.properties test.txt            test4.txt</span><br><span class="line">➜  mygit git:(new_branch) git checkout master</span><br><span class="line">Switched to branch <span class="string">'master'</span></span><br><span class="line">➜  mygit git:(master) ls</span><br><span class="line">mydir               settings.properties test.txt</span><br></pre></td></tr></table></figure>
<p>如果想在2个分支之间来回切换，可以使用<code>git checkout -</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(new_branch) git checkout -</span><br><span class="line">Switched to branch <span class="string">'master'</span></span><br><span class="line">➜  mygit git:(master) git checkout -</span><br><span class="line">Switched to branch <span class="string">'new_branch'</span></span><br><span class="line">➜  mygit git:(new_branch) git checkout -</span><br><span class="line">Switched to branch <span class="string">'master'</span></span><br><span class="line">➜  mygit git:(master) git checkout -</span><br><span class="line">Switched to branch <span class="string">'new_branch'</span></span><br></pre></td></tr></table></figure>
<h4 id="删除分支"><a href="#删除分支" class="headerlink" title="删除分支"></a>删除分支</h4><p>分支一样可以删除，命令：<code>git branch -d new_branch</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(new_branch) git branch -d new_branch</span><br><span class="line">error: Cannot delete branch <span class="string">'new_branch'</span> checked out at <span class="string">'/Users/xiaomai/code/zhanglong/git/mygit</span></span><br></pre></td></tr></table></figure>
<p>报错了，为什么呢？因为你当前本来就在<strong>new_branch</strong>上，就好比你坐到一个凳子上怎么能把凳子挪走呢。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(new_branch) git checkout -</span><br><span class="line">Switched to branch <span class="string">'master'</span></span><br><span class="line">➜  mygit git:(master) git branch -d new_branch</span><br><span class="line">error: The branch <span class="string">'new_branch'</span> is not fully merged.</span><br><span class="line">If you are sure you want to delete it, run <span class="string">'git branch -D new_branch'</span>.</span><br></pre></td></tr></table></figure>
<p>又出错了，说<strong>new_branch</strong>并没有合并过，不能删除，如果要删除要使用<code>git branch -D new_branch</code>。</p>
<p>因为我们之前创建了test4.txt这个文件，它是存在于new_branch分支上的，如果直接删除这个分支，那么在这个分支上所做的修改就会丢失了，也就是test4.txt这个文件就没了。</p>
<p>现在来测试一下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) git branch -D new_branch</span><br><span class="line">Deleted branch new_branch (was 8013cc2).</span><br></pre></td></tr></table></figure>
<p>下面的新创建了一个new_branch2分支，但是这次却可以直接删除，因为这2个分支是一样的，没有区别，不需要合并。 </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) git branch new_branch2</span><br><span class="line">➜  mygit git:(master) git checkout new_branch2  </span><br><span class="line">Switched to branch &apos;new_branch2&apos;</span><br><span class="line">➜  mygit git:(new_branch2) git checkout -</span><br><span class="line">Switched to branch &apos;master&apos;</span><br><span class="line">➜  mygit git:(master) git branch -d new_branch2  </span><br><span class="line">Deleted branch new_branch2 (was c7a801e).</span><br></pre></td></tr></table></figure>
<h4 id="创建并切换"><a href="#创建并切换" class="headerlink" title="创建并切换"></a>创建并切换</h4><p>创建分支并切换，我们之前是用了2个命令，Git也提供了简便的方式，用一条命令来完成这个操作。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) git checkout -b new_branch4</span><br><span class="line">Switched to a new branch <span class="string">'new_branch4'</span></span><br></pre></td></tr></table></figure>
<h4 id="合并"><a href="#合并" class="headerlink" title="合并"></a>合并</h4><p>刚才创建分支后删除，有提示到说没有合并，我们再创建一个分支，并对文件做一些修改。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(new_branch4) vi test.txt </span><br><span class="line">➜  mygit git:(new_branch4) ✗ git status</span><br><span class="line">On branch new_branch4</span><br><span class="line">Changes not staged <span class="keyword">for</span> commit:</span><br><span class="line">  (use <span class="string">"git add &lt;file&gt;..."</span> to update what will be committed)</span><br><span class="line">  (use <span class="string">"git checkout -- &lt;file&gt;..."</span> to discard changes <span class="keyword">in</span> working directory)</span><br><span class="line"></span><br><span class="line">	modified:   test.txt</span><br><span class="line"></span><br><span class="line">no changes added to commit (use <span class="string">"git add"</span> and/or <span class="string">"git commit -a"</span>)</span><br><span class="line">➜  mygit git:(new_branch4) ✗ git add .</span><br><span class="line">➜  mygit git:(new_branch4) ✗ git commit -m <span class="string">'add a new line in test'</span></span><br><span class="line">[new_branch4 41c3327] add a new line <span class="keyword">in</span> <span class="built_in">test</span></span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br><span class="line">➜  mygit git:(new_branch4) git checkout -</span><br><span class="line">Switched to branch <span class="string">'master'</span></span><br><span class="line">➜  mygit git:(master) git branch</span><br><span class="line">* master</span><br><span class="line">  new_branch4</span><br></pre></td></tr></table></figure>
<p>Git里合并的命令是<code>git merge</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) git merge new_branch4  </span><br><span class="line">Updating c7a801e..41c3327</span><br><span class="line">Fast-forward</span><br><span class="line"> test.txt | 1 +</span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br></pre></td></tr></table></figure>
<p>这个命令的意思是，将<strong>new_branch4</strong>分支上的修改合并到<strong>master</strong>上。</p>
<p>现在查看test.txt的文件内容，和我们在<strong>new_branch4</strong>上修改的内容一样了。</p>
<p>再验证一下合并之后能不能成功删除。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) git branch -d new_branch4  </span><br><span class="line">Deleted branch new_branch4 (was 41c3327).</span><br></pre></td></tr></table></figure>
<p>因为<strong>new_branch4</strong>分支上的修改已经合并（merge）到master上了，所以可以正常删除。</p>
<h3 id="分支的原理"><a href="#分支的原理" class="headerlink" title="分支的原理"></a>分支的原理</h3><p><img src="/img/git/git2-1.png" alt=""></p>
<p>分支到底是什么呢，分支指的是一个commit对象链，它就像是一条<strong><em>工作记录线</em></strong>（或者时间线），这张图有4个commit，这4个commit共同组成了一个branch。</p>
<p>每一次提交都会记录上一次的commit id，我们看第一个提交的commit id是10dc7…，而第二次提交的parent属性正好是10dc7。而这也就把所有的commit连接起来了，在版本库只有一个分支的情况下，这些连接起来的线也就组成了分支的信息。</p>
<h3 id="HEAD"><a href="#HEAD" class="headerlink" title="HEAD"></a>HEAD</h3><p>之前我们在使用<code>git rest HEAD</code>的时候，这个HEAD到底是什么意思呢？</p>
<p>HEAD指向的是当前分支，master指向提交，什么意思呢，如下图所示，master是指向第3个提交，而HEAD是指向的master分支。</p>
<p><img src="/img/git/git2-2.png" alt=""></p>
<p>下图就相当于在master分支上执行<code>git checkout -b dev</code>，创建并切换到dev分支：创建了一个叫dev的指针，它和master一样也指向同一个提交，并且HEAD指向当前分支上，也就是dev。</p>
<p><img src="/img/git/git2-3.png" alt=""></p>
<p>这个我们可以通过命令来实战验证一下。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) git checkout -b dev</span><br><span class="line">Switched to a new branch <span class="string">'dev'</span></span><br><span class="line">➜  mygit git:(dev) cat .git/HEAD </span><br><span class="line">ref: refs/heads/dev</span><br></pre></td></tr></table></figure>
<p>这张图又什么意思呢，相信很容易猜到了，就是在dev分支上产生了一次提交。</p>
<p><img src="/img/git/git2-4.png" alt=""></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(dev) vi test.txt </span><br><span class="line">➜  mygit git:(dev) ✗ git add .</span><br><span class="line">➜  mygit git:(dev) ✗ git commit -m &apos;add a new line in test.txt&apos;</span><br><span class="line">[dev bd1bf5d] add a new line in test.txt</span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br></pre></td></tr></table></figure>
<p>下面的图意思是将dev合并到master分支上，因为master没有修改，所以这里不会产生冲突，直接将master的指针指向了第4个提交（这种没有冲突的提交也叫Fast-Forward）</p>
<p><img src="/img/git/git2-5.png" alt=""></p>
<p><code>git log -3</code></p>
<p>先看一下dev分支的提交日志：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">commit bd1bf5d33911594452c43f990904f393f6037607 (HEAD -&gt; dev)</span><br><span class="line">Author: 杨帆 &lt;test@test.com&gt;</span><br><span class="line">Date:   Thu Sep 21 15:14:45 2017 +0800</span><br><span class="line"></span><br><span class="line">    add a new line in test.txt</span><br><span class="line"></span><br><span class="line">commit 41c3327a9cf94bcb85799f0253fba0707036292c (master)</span><br><span class="line">Author: 杨帆 &lt;test@test.com&gt;</span><br><span class="line">Date:   Thu Sep 21 14:29:37 2017 +0800</span><br><span class="line"></span><br><span class="line">    add a new line in test</span><br></pre></td></tr></table></figure>
<hr>
<p>再看一下master分支的提交日志：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">commit 41c3327a9cf94bcb85799f0253fba0707036292c (HEAD -&gt; master)</span><br><span class="line">Author: 杨帆 &lt;test@test.com&gt;</span><br><span class="line">Date:   Thu Sep 21 14:29:37 2017 +0800</span><br><span class="line"></span><br><span class="line">    add a new line in test</span><br><span class="line"></span><br><span class="line">commit c7a801e2d02195a49d6741d61f1e6b471664b3f6</span><br><span class="line">Author: 杨帆 &lt;test@test.com&gt;</span><br><span class="line">Date:   Thu Sep 21 11:06:54 2017 +0800</span><br></pre></td></tr></table></figure>
<p>可以看到dev的第二个提交正好是master分支的第一个提交。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) git merge dev</span><br><span class="line">Updating 41c3327..bd1bf5d</span><br><span class="line">Fast-forward</span><br><span class="line"> test.txt | 1 +</span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br></pre></td></tr></table></figure>
<p>现在执行merge操作，可以看到update 41c3327，也就是dev的第一条提交，正好是master没有的，结合刚才的图来看，也就是把master指向了41c3327。</p>
<hr>
<p>看一下merge之后master分支上的<code>git log -3</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">commit bd1bf5d33911594452c43f990904f393f6037607 (HEAD -&gt; master, dev)</span><br><span class="line">Author: 杨帆 &lt;test@test.com&gt;</span><br><span class="line">Date:   Thu Sep 21 15:14:45 2017 +0800</span><br><span class="line"></span><br><span class="line">    add a new line in test.txt</span><br><span class="line"></span><br><span class="line">commit 41c3327a9cf94bcb85799f0253fba0707036292c</span><br><span class="line">Author: 杨帆 &lt;test@test.com&gt;</span><br><span class="line">Date:   Thu Sep 21 14:29:37 2017 +0800</span><br><span class="line"></span><br><span class="line">    add a new line in test</span><br><span class="line"></span><br><span class="line">commit c7a801e2d02195a49d6741d61f1e6b471664b3f6</span><br><span class="line">Author: 杨帆 &lt;test@test.com&gt;</span><br><span class="line">Date:   Thu Sep 21 11:06:54 2017 +0800</span><br><span class="line"></span><br><span class="line">    gitignore</span><br><span class="line">(END)</span><br></pre></td></tr></table></figure>
<hr>
<p>下面再看看我们在master和dev分支上都作一些修改，然后合并。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) <span class="built_in">echo</span> <span class="string">'hello java'</span> &gt; test2.txt </span><br><span class="line">➜  mygit git:(master) ✗ git add .</span><br><span class="line">➜  mygit git:(master) ✗ git commit -m <span class="string">'create a file test2.txxt'</span></span><br><span class="line">[master bc84a7f] create a file test2.txxt</span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br><span class="line"> create mode 100644 test2.txt</span><br><span class="line">➜  mygit git:(master) git checkout dev</span><br><span class="line">Switched to branch <span class="string">'dev'</span></span><br><span class="line">➜  mygit git:(dev) ✗ <span class="built_in">echo</span> <span class="string">'hello kotlin'</span> &gt; test2.txt </span><br><span class="line">➜  mygit git:(dev) ✗ git add .</span><br><span class="line">➜  mygit git:(dev) ✗ git commit -m <span class="string">'create file test2.txt'</span></span><br><span class="line">[dev def3afc] create file test2.txt</span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br><span class="line"> create mode 100644 test2.txt</span><br></pre></td></tr></table></figure>
<p>现在我们在master和dev上都创建了一个<code>test2.txt</code>，并且内容不一样。</p>
<p>现在master和dev指向的commit都不一样，现在合并：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) git merge dev</span><br><span class="line">Auto-merging test2.txt</span><br><span class="line">CONFLICT (add/add): Merge conflict <span class="keyword">in</span> test2.txt</span><br><span class="line">Automatic merge failed; fix conflicts and <span class="keyword">then</span> commit the result.</span><br></pre></td></tr></table></figure>
<p>出现了冲突，git不知道如何处理这种冲突，需要我们自己解决，我们现在看一下<strong>test2.txt</strong>的内容是什么<br><code>vi test2.txt</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</span><br><span class="line">hello java</span><br><span class="line">=======</span><br><span class="line">hello kotlin</span><br><span class="line">&gt;&gt;&gt;&gt;&gt;&gt;&gt; dev</span><br></pre></td></tr></table></figure>
<p>上面是HEAD（当前分支master）的内容，下面的dev分支的内容，现在我们手动处理一下冲突，编辑保留我们需要留下的内容。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) ✗ vi test2.txt </span><br><span class="line">➜  mygit git:(master) ✗ git status</span><br><span class="line">On branch master</span><br><span class="line">You have unmerged paths.</span><br><span class="line">  (fix conflicts and run <span class="string">"git commit"</span>)</span><br><span class="line">  (use <span class="string">"git merge --abort"</span> to abort the merge)</span><br><span class="line"></span><br><span class="line">Unmerged paths:</span><br><span class="line">  (use <span class="string">"git add &lt;file&gt;..."</span> to mark resolution)</span><br><span class="line"></span><br><span class="line">	both added:      test2.txt</span><br><span class="line"></span><br><span class="line">no changes added to commit (use <span class="string">"git add"</span> and/or <span class="string">"git commit -a"</span>)</span><br></pre></td></tr></table></figure>
<p>现在执行<code>git add test2.txt</code>表示冲突已经解决，可以提交</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) ✗ git add test2.txt </span><br><span class="line">➜  mygit git:(master) ✗ git status</span><br><span class="line">On branch master</span><br><span class="line">All conflicts fixed but you are still merging.</span><br><span class="line">  (use <span class="string">"git commit"</span> to conclude merge)</span><br><span class="line"></span><br><span class="line">Changes to be committed:</span><br><span class="line"></span><br><span class="line">	modified:   test2.txt</span><br><span class="line"></span><br><span class="line">➜  mygit git:(master) ✗ git commit</span><br><span class="line">[master b4bd64b] Merge branch <span class="string">'dev'</span></span><br><span class="line">➜  mygit git:(master) git status</span><br><span class="line">On branch master</span><br><span class="line">nothing to commit, working tree clean</span><br><span class="line">➜  mygit git:(master) cat test2.txt </span><br><span class="line">hello java</span><br><span class="line">hello kotlin</span><br></pre></td></tr></table></figure>
<p>现在再回过头来看看dev分支下test2.txt的内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(master) git checkout dev</span><br><span class="line">Switched to branch <span class="string">'dev'</span></span><br><span class="line">➜  mygit git:(dev) cat test2.txt </span><br><span class="line">hello kotlin</span><br></pre></td></tr></table></figure>
<p>内容还是跟之前的一样，如果这个时候我们执行<code>git merge master</code>，还会产生冲突吗？</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">➜  mygit git:(dev) git merge master</span><br><span class="line">Updating def3afc..b4bd64b</span><br><span class="line">Fast-forward</span><br><span class="line"> test2.txt | 1 +</span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br><span class="line">➜  mygit git:(dev) cat test2.txt </span><br><span class="line">hello java</span><br><span class="line">hello kotlin</span><br></pre></td></tr></table></figure>
<p>没有冲突，fast-forward直接成功，为什么呢？</p>
<p>因为在master上合并后，指针移动了2次，一次是dev的提交，一次是合并的提交。</p>
<p>现在在dev上再将master合并过来的时候，master的提交里已经包含了dev这次的提交，git检测到没有冲突，直接将指针指向了master的提交就可以了。</p>
<h2 id="fast-forward"><a href="#fast-forward" class="headerlink" title="fast-forward"></a>fast-forward</h2><ul>
<li>如果可能，合并分支时Git会使用fast-forward模式</li>
<li>在这种模式下，删除分支时会丢掉分支信息</li>
<li>合并时加上–no-ff参数会禁用fast-forward，这样会多出一个commit id （Merge branch ‘dev’）<ul>
<li><code>git merge --no-ff dev</code></li>
</ul>
</li>
<li>查看log<ul>
<li><code>git log --graph</code></li>
</ul>
</li>
</ul>
<h2 id="版本回退"><a href="#版本回退" class="headerlink" title="版本回退"></a>版本回退</h2><p>刚才已经讲解了分支的原理，那么现在再来理解一下版本回退，应该就很容易想到了，无非就是移动分支的指针某到一个提交上。</p>
<ul>
<li>回退到上一版本<ul>
<li><code>git reset --hard HEAD^</code>这个命令表示回退到<code>HEAD</code>的上一个版本，多一个<code>^</code>就多回退一个版本。</li>
<li><code>git reset --hard HEAD~2</code>表示回退到<code>HEAD</code>的某一个版本之前，后面的数字就是回退的数量。</li>
<li><code>git reset --hard commit id</code></li>
</ul>
</li>
<li>返回到某一个版本<ul>
<li><code>git reflog</code></li>
</ul>
</li>
</ul>
<p>如果现在已经在历史的某一个提交上，用<code>git log</code>是看不到之后的commit id的。那么就可以使用<code>git reflog</code>查看，<code>git reflog</code>显示的是操作日志。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">ca07f7b (HEAD -&gt; master) HEAD@&#123;0&#125;: reset: moving to ca07f7b</span><br><span class="line">0d2246f HEAD@&#123;1&#125;: reset: moving to HEAD</span><br><span class="line">0d2246f HEAD@&#123;2&#125;: reset: moving to HEAD</span><br><span class="line">0d2246f HEAD@&#123;3&#125;: reset: moving to HEAD^^</span><br><span class="line">3240097 HEAD@&#123;4&#125;: reset: moving to HEAD^</span><br><span class="line">ca07f7b (HEAD -&gt; master) HEAD@&#123;5&#125;: commit: add another line</span><br><span class="line">3240097 HEAD@&#123;6&#125;: commit: add another line</span><br><span class="line">d8f57a9 HEAD@&#123;7&#125;: commit: add a new line</span><br><span class="line">0d2246f HEAD@&#123;8&#125;: commit (initial): initial commit</span><br><span class="line">(END)</span><br></pre></td></tr></table></figure>
      
    </div>

    

    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/img/donate/wechatpay.jpeg" alt="杨帆 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/img/donate/alipay.jpeg" alt="杨帆 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/git/" rel="tag"># git</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/13/netty6/" rel="next" title="Netty-源码分析(一)">
                <i class="fa fa-chevron-left"></i> Netty-源码分析(一)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/22/netty7/" rel="prev" title="Netty-源码分析(二)">
                Netty-源码分析(二) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8yOTk2Ni82NTMx"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/img/photo/bug.png"
                alt="杨帆" />
            
              <p class="site-author-name" itemprop="name">杨帆</p>
              <p class="site-description motion-element" itemprop="description">记录工作和学习中遇到的问题</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">107</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">18</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">38</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/sail-y" target="_blank" title="GitHub"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://weibo.com/338632221" target="_blank" title="微博"><i class="fa fa-fw fa-globe"></i>微博</a>
                  
                </span>
              
            </div>
          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#gitignore"><span class="nav-number">1.</span> <span class="nav-text">.gitignore</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#规则"><span class="nav-number">1.1.</span> <span class="nav-text">规则</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Git分支"><span class="nav-number">2.</span> <span class="nav-text">Git分支</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#git-branch"><span class="nav-number">2.1.</span> <span class="nav-text">git branch</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建分支"><span class="nav-number">2.1.1.</span> <span class="nav-text">创建分支</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#切换分支"><span class="nav-number">2.1.2.</span> <span class="nav-text">切换分支</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#删除分支"><span class="nav-number">2.1.3.</span> <span class="nav-text">删除分支</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#创建并切换"><span class="nav-number">2.1.4.</span> <span class="nav-text">创建并切换</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#合并"><span class="nav-number">2.1.5.</span> <span class="nav-text">合并</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分支的原理"><span class="nav-number">2.2.</span> <span class="nav-text">分支的原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HEAD"><span class="nav-number">2.3.</span> <span class="nav-text">HEAD</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#fast-forward"><span class="nav-number">3.</span> <span class="nav-text">fast-forward</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#版本回退"><span class="nav-number">4.</span> <span class="nav-text">版本回退</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">杨帆</span>

  

  
</div>




  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/theme-next/hexo-theme-next">NexT.Gemini</a> v6.1.0</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv" title="总访客量">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  

  
    <span class="site-pv" title="总访问量">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>









        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>














  



  
  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three-waves.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.1.0"></script>



  



	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  










  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  

  
  

  

  

  

  

</body>
</html>
